/**
 * Created by Administrator on 2018/7/31.
 */
//input验证
$("input").blur(function(){
    verify(this);
});

//验证
var verify = function(obj){
    var tab = $(obj).attr('name');//获取标志
    var val = $(obj).val();
    //用户名
    if(tab == 'username'){
        var res = /[\u4E00-\u9FA5]{2,5}(?:·[\u4E00-\u9FA5]{2,5})*/.test(val);
        html(obj,res,'真实姓名格式错误！');
    }
    //学号检测
    else if(tab == "params[student_id]"){
        var res = /^\d{11}$/.test(val);
        html(obj,res,'学号格式错误！');
    }
    //邮箱验证
    else if(tab == 'email'){
        var res = /^\w+(?:[-+.']\w+)*@\w+(?:[-.]\w+)*\.\w+(?:[-.]\w+)*$/.test(val);
        html(obj,res,'邮箱格式错误！');
    }else if(tab == 'number'){
        var res = /^(0|86|17951)?(13[0-9]|15[012356789]|17[0-9]|18[0-9]|14[57])[0-9]{8}$/.test(val);
        html(obj,res,'手机号格式错误！');
    }else if(tab == 'password'){
        if(!val){
            html(obj,res,'请输入密码！');
            return false;
        }
        var res = /^[^\u4e00-\u9fa5\s]{6,20}$/.test(val);
        html(obj,res,'密码至少为6位！');
    }else if(tab == 'password2'){
        if(!val){
            html(obj,res,'请输入密码！');
            return false;
        }
        var val2 = $("input[name='password']").val();
        res = (val == val2) ? true : false;
        html(obj,res,'两次密码输入不一致！');
    }
}

//正则检测
var html = function(obj,res,val){
    (res) ? $(obj).parent().next().html("<svg height='20px' width='20px' aria-hidden='true'><use xlink:href='#icon-tongguo'></use></svg>") : $(obj).parent().next().html(val).css('color','#E9686B');
}


//点击获取号码
$("#sms").click(function(){
    if($('#sms').hasClass("btn-retransmit-disabled")) return false;
    dysms();
})

//获取验证码
var dysms = function(){
    //获取号码
    var number = $("input[name='number']").val();
    var tab = $("#code").attr('tab');
    //异步提交
    $.post('{{"index.php?app=Login&sms"|URL}}',{'number':number,'tab':tab},function(result){
        if(result.statusCode=='200'){
            set_time(1);
            ui.success('短信发送成功!');
            $("#number").html("<svg height='20px' width='20px' aria-hidden='true'><use xlink:href='#icon-tongguo'></use></svg>");
            setCookie('sms',result.message,'5');
            $('#sms').addClass("btn-retransmit-disabled");
        }else if(result.statusCode == '300') $("#"+result.message.name+"").html(result.message.message).css('color','#E9686B');
    },'json');
}

var vod_show;
//设置倒计时(分钟)
function set_time(min){
    vod_show=setInterval(show_time,100);
    document.endtime = new Date();
    document.endtime = document.endtime.getTime();
    document.endtime = parseInt(document.endtime)+(min*60*1000);
}
function show_time()
{
    var nowtime = new Date();
    var leftsecond=parseInt((document.endtime-nowtime.getTime())/1000);
    s=parseInt(leftsecond);
    var c=new Date();
    $('#sms').html(" 已发送（"+s+"）");
    if(leftsecond<=0){
        $('#sms').removeClass("btn-retransmit-disabled");
        $('#sms').css("cursor",'pointer');
        $('#sms').html("获取短信验证码");
        clearInterval(vod_show);
    }
}
function setCookie(cname,cvalue,fen){
    var d = new Date();
    d.setTime(d.getTime()+(fen*60*1000));
    var expires = "expires="+d.toGMTString();
    document.cookie = cname+"="+cvalue+"; "+expires;
}
function getCookie(cname){
    var name = cname + "=";
    var ca = document.cookie.split(';');
    for(var i=0; i<ca.length; i++) {
        var c = ca[i].trim();
        if (c.indexOf(name)==0) { return c.substring(name.length,c.length); }
    }
    return "";
}

//验证
function register(){
    if(!$('#sms').hasClass("btn-retransmit-disabled")){
        $("#number").html('请获取验证码!').css('color','#E9686B');
        return false;
    }
    //获取短信验证码
    var sms = getCookie("sms");
    //获取输入的验证码
    var code = $("#code").val();
    //验证码正确与否
    if(sms == code && sms !=''){
        post();
    }else if(!$("input[name='number']").val()){
        $("#number").html('手机号码错误!').css('color','#E9686B');
        return false;
    }else if(!$('#sms').hasClass("btn-retransmit-disabled")){
        $("#number").html('请获取验证码!').css('color','#E9686B');
        return false;
    }else{
        $("#number").html('验证码错误!').css('color','#E9686B');
        $("#ver").val("");
        return false;
    }
};